import os
import struct

import numpy as np
import os

def ascll_to_binary(ascii_file_path, binary_file_path):
    # 读取ASCII STL文件
    if os.path.exists(binary_file_path):
        return
    with open(ascii_file_path, 'r') as file:
        lines = file.readlines()
    del lines[0]  # 去除文件头
    del lines[-1]  # 去除文件尾部
    # 构建二进制文件内容
    header = b''  # 80字节的文件头
    for i in range(80):
        header += b'\x00'  # 填充字节
    triangles = b''  # 存储所有三角面片的数据
    trinum=int(len(lines)/7)
    triangles += struct.pack('I', trinum)  # 写入三角面片数量
    for i in range(trinum):
        normal=lines[i*7].split()[2:]
        triangles += struct.pack('3f', float(normal[0]), float(normal[1]), float(normal[2]))
        for j in range(3):
            vertices = lines[i*7+2+j].split()[1:]
            triangles += struct.pack('3f', float(vertices[0]), float(vertices[1]), float(vertices[2]))
        triangles += b'\x00\x00'  # 属性字节

    # 写入二进制文件
    with open(binary_file_path, 'wb') as file:
        file.write(header)
        file.write(triangles)

